package com.cn.jinl.service;

import com.cn.jinl.api.IOnDutyBgTaskService;
import com.cn.jinl.api.IOnDutyReduceService;
import com.cn.jinl.dao.OnDutyReduceMapper;
import com.cn.jinl.dao.OnDutyReducePersonMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class OnDutyReduceServiceImpl implements IOnDutyReduceService {

    @Autowired
    private OnDutyReduceMapper onDutyReduceMapper;

    @Autowired
    private IOnDutyBgTaskService onDutyBgTaskService;

    @Autowired
    private OnDutyReducePersonMapper onDutyReducePersonMapper;

    @Override
    public List<OnDutyReduce> getOnDutyReduceList(QueryForm queryForm) throws Exception {
        int count = onDutyReduceMapper.selectOndutyReduceListCount(queryForm);
        List<OnDutyReduce> result = onDutyReduceMapper.selectOndutyReduceList(queryForm);
        if(CollectionsUtil.isNotEmpty(result)){
            result.get(0).setCount(count);
        }
        for(OnDutyReduce reduce:result){
            String dutyDayTask = reduce.getDutyDayTask();
            String whiteDayTask = reduce.getWhiteDayTask();
            dutyDayTask = dutyDayTask+" "+(StringUtils.equals("1",whiteDayTask)?"白班":"晚班");
            reduce.setDutyDayTask(dutyDayTask);
        }
        return result;
    }

    @Override
    public List<OnDutyReduce> getOnDutyReduceDetailList(QueryForm queryForm) throws Exception {
        int count = onDutyReduceMapper.selectOndutyReduceDetailsCount(queryForm);
        List<OnDutyReduce> result = onDutyReduceMapper.selectOndutyReduceDetails(queryForm);
        if(CollectionsUtil.isNotEmpty(result)){
            result.get(0).setCount(count);
        }
        for(OnDutyReduce reduce:result){
            String dutyDayTask = reduce.getDutyDayTask();
            String whiteDayTask = reduce.getWhiteDayTask();
            dutyDayTask = dutyDayTask+" "+(StringUtils.equals("1",whiteDayTask)?"白班":"晚班");
            reduce.setDutyDayTask(dutyDayTask);
        }
        return result;
    }

    @Override
    public String addOnDutyReduce(OnDutyReduce onDutyReduce) throws Exception {
        if(this.countOnDutyReduceByBgId(onDutyReduce.getBgId()) > 0){
            return "任务已在扣减中，无法再次创建";
        }
        onDutyReduce.setId(CommonUtil.generateRandomNum("reduce-"));
        int count = onDutyReduceMapper.insertSelective(onDutyReduce);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOnDutyReduce(OnDutyReduce onDutyReduce) throws Exception {
        String id = onDutyReduce.getId();
        String deleteCount = onDutyReduce.getDeleteCount();
        if(Integer.parseInt(deleteCount) < 1){
            return "扣减件数必须大于0";
        }
        String bgTaskId = onDutyReduce.getBgId();
        if(StringUtils.isEmpty(bgTaskId)){
            return "缺少任务id";
        }
        OnDutyBgTask task = onDutyBgTaskService.getOnDutyBgTaskListById(bgTaskId);
        if(task == null){
            return "任务不存在";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(new Date());
        onDutyReduce.setApplyTime(dateStr);
        onDutyReduce.setWhiteDay(task.getWhiteDay());
        onDutyReduce.setDutyDay(task.getDutyDay());
        if(StringUtils.isEmpty(id)){
            return this.addOnDutyReduce(onDutyReduce);
        }
        OnDutyReduceExample example = new OnDutyReduceExample();
        example.createCriteria().andIdEqualTo(id);
        int count = onDutyReduceMapper.updateByExampleSelective(onDutyReduce,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateCheckOnDutyReduce(OnDutyReduce onDutyReduce) throws Exception {
        List<OnDutyReducePerson> onDutyReducePeoples = onDutyReduce.getOnDutyReducePeoples();
        if(CollectionUtils.isEmpty(onDutyReducePeoples)){
            return "扣减人不能为空！";
        }
        for(OnDutyReducePerson person : onDutyReducePeoples){
            if(Double.parseDouble(person.getReducePrice()) < 0){
                return "扣减金额不能小于0";
            }
        }
        String id = onDutyReduce.getId();
        String bgId = onDutyReduce.getBgId();
        if(StringUtils.isEmpty(id)||StringUtils.isEmpty(bgId)){
            return "参数异常";
        }
        OnDutyReducePersonExample peopleExample = new OnDutyReducePersonExample();
        peopleExample.createCriteria().andReduceIdEqualTo(id);
        int existPeopleCount = onDutyReducePersonMapper.countByExample(peopleExample);
        if(existPeopleCount > 0){
            return "已经存在扣减人员信息，请联系管理员";
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = simpleDateFormat.format(new Date());
        onDutyReduce.setCheckTime(dateStr);
        onDutyReduce.setDeduceStatus("1");
        OnDutyReduceExample example = new OnDutyReduceExample();
        example.createCriteria().andIdEqualTo(id);

        int count = onDutyReduceMapper.updateByExampleSelective(onDutyReduce,example);
        if(count < 0){
            return "更新表异常！";
        }

        count = onDutyReducePersonMapper.batchInsertReducePeople(onDutyReducePeoples);
        if(count < 1){
            return "更新表异常！!";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteOnDutyReduce(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "主键不能为空";
        }
        OnDutyReduceExample example = new OnDutyReduceExample();
        example.createCriteria().andIdEqualTo(id);
        OnDutyReduce update  = new OnDutyReduce();
        update.setStatus(Constants.DEL_STATUS);
        int count = onDutyReduceMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, Integer> getOnDutyReduceCount() throws Exception {
        OnDutyReduceExample example = new OnDutyReduceExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS);
        List<OnDutyReduce> onDutyReduces = onDutyReduceMapper.selectByExample(example);
        int unApproveCount = 0;
        int approveCount = 0;
        for(OnDutyReduce onDutyReduce:onDutyReduces){
            if(StringUtils.equals("0",onDutyReduce.getDeduceStatus())){
                ++unApproveCount;
            }else{
                ++approveCount;
            }
        }
        Map<String,Integer>resultMap = Maps.newHashMap();
        resultMap.put("unApproveCount",unApproveCount);
        resultMap.put("approveCount",approveCount);
        return resultMap;
    }

    private int countOnDutyReduceByBgId(String bgId){
        OnDutyReduceExample example = new OnDutyReduceExample();
        example.createCriteria().andBgIdEqualTo(bgId).andStatusEqualTo(Constants.NORMAL_STATUS);
        return onDutyReduceMapper.countByExample(example);
    }
}
